package impl

import "adam2/internal/model"

type Real9OptimizeAccountDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetReal9OptimizeAccountDaoImpl() *Real9OptimizeAccountDaoImpl {
	return &Real9OptimizeAccountDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (r *Real9OptimizeAccountDaoImpl) FindTableName() string {
	return "REAL9_O_ACCOUNT"
}

func (r *Real9OptimizeAccountDaoImpl) FindAll() model.Real9OptimizeAccountArray {
	var real9OptimizeAccountArray model.Real9OptimizeAccountArray
	r.db.Table("real9_o_account").Select("*").Order("id_ asc").Find(&real9OptimizeAccountArray)
	return real9OptimizeAccountArray
}

// 更新real9_o_account表的hold_stock_number、stock_assets、total_assets字段
func (r *Real9OptimizeAccountDaoImpl) UpdateBeforeSell(holdStockNumber int, stockAssets float64, accountName string) {
	r.db.Exec("update real9_o_account t "+
		"set t.hold_stock_number=?, t.stock_assets = ?, t.total_assets = round(t.capital_assets + ?, 4) where t.account_name = ?",
		holdStockNumber, stockAssets, stockAssets, accountName)
}

// 更新real9_o_account表的hold_stock_number、stock_assets、capital_assets、total_assets、total_stamp_duty、total_registrate_fee_when_sell、total_commission_when_sell字段
func (r *Real9OptimizeAccountDaoImpl) UpdateAfterSell(sellOrBuyStockNumber int, holdStockAssets float64, stockAssets float64, real9OptimizeAccount model.Real9OptimizeAccount) {
	r.db.Exec("update real9_o_account t "+
		"set t.hold_stock_number = ?, t.stock_assets = ?, t.capital_assets = round(t.capital_assets + ?, 4), t.total_assets = round(? + ? + ?, 4), "+
		"t.total_stamp_duty = (select COALESCE(sum(t1.stamp_duty), 0) from real9_o_stock_transact_record t1 where t1.account_name = ?), "+
		"t.total_registrate_fee_when_sell = (select COALESCE(sum(t1.registration_fee_when_sell), 0) from real9_o_stock_transact_record t1 where t1.account_name = ?), "+
		"t.total_commission_when_sell = (select COALESCE(sum(t1.commission_when_sell), 0) from real9_o_stock_transact_record t1 where t1.account_name = ?) "+
		"where t.account_name = ?", sellOrBuyStockNumber, holdStockAssets, stockAssets,
		holdStockAssets, real9OptimizeAccount.CapitalAssets, stockAssets,
		real9OptimizeAccount.AccountName, real9OptimizeAccount.AccountName, real9OptimizeAccount.AccountName, real9OptimizeAccount.AccountName)
}

// 更新real9_o_account表
func (r *Real9OptimizeAccountDaoImpl) UpdateAfterBuy(real9OptimizeAccount model.Real9OptimizeAccount, everyDate string, sellOrBuyStockNumber int, stockAssets float64, currentBuyOrSellStockAssets float64) {
	var registrationFeeWhenBuy float64
	r.db.Raw("select COALESCE(sum(t1.registration_fee_when_buy), 0) from real9_o_stock_transact_record t1 "+
		"where t1.account_name = ? and t1.sell_price is null and t1.sell_amount is null and t1.sell_date is null "+
		"and t1.buy_date=to_date(?,'yyyy-mm-dd')",
		real9OptimizeAccount.AccountName, everyDate).Scan(&registrationFeeWhenBuy)
	var commissionWhenBuy float64
	r.db.Raw("select COALESCE(sum(t1.commission_when_buy), 0) from real9_o_stock_transact_record t1 "+
		"where t1.account_name = ? and t1.sell_price is null and t1.sell_amount is null and t1.sell_date is null "+
		"and t1.buy_date=to_date(?,'yyyy-mm-dd')",
		real9OptimizeAccount.AccountName, everyDate).Scan(&commissionWhenBuy)
	var totalStampDuty float64
	r.db.Raw("select COALESCE(sum(t1.stamp_duty), 0) from real9_o_stock_transact_record t1 where t1.account_name = ?",
		real9OptimizeAccount.AccountName).Scan(&totalStampDuty)
	var totalRegistrationFeeWhenBuy float64
	r.db.Raw("select COALESCE(sum(t1.registration_fee_when_buy), 0) from real9_o_stock_transact_record t1 where t1.account_name = ?",
		real9OptimizeAccount.AccountName).Scan(&totalRegistrationFeeWhenBuy)
	var totalCommissionWhenBuy float64
	r.db.Raw("select COALESCE(sum(t1.commission_when_buy), 0) from real9_o_stock_transact_record t1 where t1.account_name = ?",
		real9OptimizeAccount.AccountName).Scan(&totalCommissionWhenBuy)
	r.db.Exec("update real9_o_account t "+
		"set t.hold_stock_number = ?, t.stock_assets = round(?, 4), "+
		"t.total_assets = round(? + t.capital_assets - ? - ? - ?, 4), "+
		// 下面这行可能有错，正确的可能是：资金资产=总资产-股票资产，否则资金资产部分可能就会出现负数。
		//"t.capital_assets = round(t.total_assets - ? - ? - ?, 4), "+
		"t.capital_assets = round(t.capital_assets - ? - ? - ?, 4), "+
		"t.total_registrate_fee_when_buy = ?,"+
		"t.total_commission_when_buy = ? "+
		"where t.account_name = ?",
		sellOrBuyStockNumber, stockAssets,
		stockAssets, currentBuyOrSellStockAssets, registrationFeeWhenBuy, commissionWhenBuy,
		currentBuyOrSellStockAssets, registrationFeeWhenBuy, commissionWhenBuy,
		totalRegistrationFeeWhenBuy,
		totalCommissionWhenBuy,
		real9OptimizeAccount.AccountName)

	// 向表quant_account_log中插入数据
	//u.db.Exec("insert into quant_account_log(DATE_, ACCOUNT_NAME, HOLD_STOCK_NUMBER, STOCK_ASSETS, "+
	//	"CAPITAL_ASSETS, TOTAL_ASSETS, TOTAL_STAMP_DUTY, TOTAL_REGISTRATE_FEE_WHEN_BUY, TOTAL_COMMISSION_WHEN_BUY, "+
	//	"TOTAL_REGISTRATE_FEE_WHEN_SELL, TOTAL_COMMISSION_WHEN_SELL) "+
	//	"select to_date(?, 'yyyy-mm-dd'), t.ACCOUNT_NAME, t.HOLD_STOCK_NUMBER, t.STOCK_ASSETS, t.CAPITAL_ASSETS, t.TOTAL_ASSETS, t.TOTAL_STAMP_DUTY, "+
	//	"t.TOTAL_REGISTRATE_FEE_WHEN_BUY, t.TOTAL_COMMISSION_WHEN_BUY, t.TOTAL_REGISTRATE_FEE_WHEN_SELL, t.TOTAL_COMMISSION_WHEN_SELL "+
	//	"from quant_account t where t.account_name = ?",
	//	transactionDate, (*quantAccount).AccountName)
}

// 根据账号名查询记录
func (r *Real9OptimizeAccountDaoImpl) FindByAccountName(accountName string) model.Real9OptimizeAccount {
	var real9OptimizeAccount model.Real9OptimizeAccount = model.Real9OptimizeAccount{}
	r.db.Raw("select * from real9_o_account t where t.account_name=?", accountName).Scan(&real9OptimizeAccount)
	return real9OptimizeAccount
}

// 清空表
func (r *Real9OptimizeAccountDaoImpl) TruncateTable() {
	r.db.Exec("truncate table real9_o_account")
}

// 根据日期，从real9_o_account_log表中向real9_o_account表中插入记录
func (r *Real9OptimizeAccountDaoImpl) InsertFromReal9OptimizeAccountLogByDate(date string) {
	r.db.Exec("insert into real9_o_account(account_name, hold_stock_number, stock_assets, capital_assets, total_assets, total_stamp_duty, total_registrate_fee_when_buy, total_commission_when_buy, total_registrate_fee_when_sell, total_commission_when_sell) "+
		"select account_name, hold_stock_number, stock_assets, capital_assets, total_assets, total_stamp_duty, total_registrate_fee_when_buy, total_commission_when_buy, total_registrate_fee_when_sell, total_commission_when_sell "+
		"from real9_o_account_log where date_=to_date(?,'yyyy-mm-dd')", date)
}

// 查询所有记录，并按照总资产降序排列
func (r *Real9OptimizeAccountDaoImpl) FindAllOrderByTotalAssetsDesc() model.Real9OptimizeAccountArray {
	var real9OptimizeAccountArray model.Real9OptimizeAccountArray
	r.db.Raw("select * from real9_o_account t order by t.total_assets desc").Scan(&real9OptimizeAccountArray)
	return real9OptimizeAccountArray
}
